﻿using System;
using System.Web.UI;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Model;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class GiftCardAttributesControl : BaseNopFrontendUserControl
    {
        private int _productVariantId = 0;

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            Load += new EventHandler(GiftCardAttributesControl_Load);
        }

        void GiftCardAttributesControl_Load(object sender, EventArgs e) {
            BindData();
        }

        protected void BindData() {
            var pv = this.ProductService.GetProductVariantById(this.ProductVariantId);
            if (pv == null || !pv.IsGiftCard) {
                this.Visible = false;
            }
            else {
                //pre-entered data
                if (NopContext.Current.User != null && !NopContext.Current.User.IsGuest) {
                    txtSenderName.Text = NopContext.Current.User.FullName;
                    txtSenderEmail.Text = NopContext.Current.User.Email;
                }

                //gift card type
                phRecipientEmail.Visible = (GiftCardTypeEnum)pv.GiftCardType == GiftCardTypeEnum.Virtual;
                phSenderEmail.Visible = (GiftCardTypeEnum)pv.GiftCardType == GiftCardTypeEnum.Virtual;
            }
        }

        public int ProductVariantId {
            get { return _productVariantId; }
            set { _productVariantId = value; }
            //get {
            //    if (ViewState["ProductVariantId"] == null)
            //        return 0;
            //    else
            //        return (int)ViewState["ProductVariantId"];
            //}
            //set {
            //    ViewState["ProductVariantId"] = value;
            //}
        }

        public string RecipientName {
            get {
                return txtRecipientName.Text;
            }
        }

        public string RecipientEmail {
            get {
                return txtRecipientEmail.Text;
            }
        }

        public string SenderName {
            get {
                return txtSenderName.Text;
            }
        }

        public string SenderEmail {
            get {
                return txtSenderEmail.Text;
            }
        }

        public string GiftCardMessage {
            get {
                return txtGiftCardMessage.Text;
            }
        }
    }
}